package service

import (
	"context"
	"episode_srv/basic/appConfig"
	__ "episode_srv/basic/proto"
	"episode_srv/handler/models"
	"errors"
	"time"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedEpisodeServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {

	var err error
	var v models.Video

	if in.Title == "" {
		return nil, errors.New("视频标题不能为空")
	}

	v.Title = in.Title
	v.TypesId = in.TypesId
	v.CoverUrl = in.CoverUrl
	v.PlayUrl = in.PlayUrl
	if err = v.CreateVideo(appConfig.DB); err != nil {
		return nil, errors.New("视频添加失败")
	}

	return &__.AddVideoResp{
		VideoId: int64(v.ID),
	}, nil
}

func (s *Server) AddEpisode(_ context.Context, in *__.AddEpisodeReq) (*__.AddEpisodeResp, error) {
	var err error
	var e models.Episode
	var ec models.Correlated

	if in.Title == "" {
		return nil, errors.New("电视剧标题不能为空")
	}

	tx := appConfig.DB.Begin()

	e.IsVip = in.IsVip
	e.Title = in.Title
	e.AuthorId = in.AuthorId
	e.TypesId = in.EpisodeNo
	e.EpisodeNo = in.EpisodeNo
	if err = e.CreateEpisode(tx); err != nil {
		tx.Rollback()
		return nil, errors.New("电视剧添加失败")
	}

	ec.EpisodeId = int64(e.ID)
	ec.VideoId = in.VideoId
	ec.NumNo = in.NumNo
	if err = ec.CreateCorrelated(tx); err != nil {
		tx.Rollback()
		return nil, errors.New("关联分集信息表失败")
	}

	tx.Commit()

	return &__.AddEpisodeResp{
		EpisodeId: int64(e.ID),
		Msg:       "电视剧添加成功，剧集关联成功",
	}, nil
}

func (s *Server) CheckVideo(_ context.Context, in *__.CheckVideoReq) (*__.CheckVideoResp, error) {
	var err error
	var v models.Video
	var msg string

	//err = appConfig.DB.Where("id=?").Update("check_reason", in.CheckReason).Error
	//if err != nil {
	//	return nil, errors.New("审核备注添加失败")
	//}

	if err = v.UpdateCheckStatus(appConfig.DB, in.VideoId, in.CheckStatus); err != nil {
		return nil, errors.New("审核状态修改失败")
	}

	if err = v.GetVideoById(appConfig.DB, int64(v.ID)); err != nil {
		return nil, errors.New("视频信息获取失败")
	}
	change(msg)

	return &__.CheckVideoResp{
		VideoId: in.VideoId,
		Mag:     "驳回",
	}, nil
}

func change(msg string) string {
	var v models.Video

	if v.CheckStatus == 0 {
		msg = "待审核"
	} else if v.CheckStatus == 1 {
		msg = "审核中"
	} else if v.CheckStatus == 2 {
		msg = "审核通过"
	} else if v.CheckStatus == 3 {
		msg = "驳回"
	}

	return msg
}

func (s *Server) MemberCheck(_ context.Context, in *__.MemberCheckReq) (*__.MemberCheckResp, error) {

	var err error
	var u models.User
	var e models.Episode

	if err = e.GetEpsById(appConfig.DB, in.EpsId); err != nil {
		return nil, errors.New("电视剧信息获取失败")
	}

	if err = u.GetUserById(appConfig.DB, int(in.UserId)); err != nil {
		return nil, errors.New("用户信息获取失败")
	}

	if u.ID == 0 {
		return nil, errors.New("用户未登录！")
	}

	if u.MemberStatus != 2 {
		return nil, errors.New("用户不是会员！")
	}

	Now := time.Now().Format(time.RFC3339)
	if u.MemberExpiredAt < Now {
		return nil, errors.New("VIP已过期！")
	}

	return &__.MemberCheckResp{
		UserId: in.UserId,
		Msg:    "该用户在VIP有效期内",
	}, nil
}
